import 'package:merchants/services/base-service.dart';
import 'package:merchants/utils/we-chat-auth-result.dart';

class WeChatService extends BaseService {
  WeChatService({required super.ref});

  Future<WeChatAuthResult> getUserInfoWithCode(String code) async {
    print('DEBUG: WeChatService.getUserInfoWithCode 开始，code: $code');
    final response = await post<Map<String, dynamic>>(
      '/wechat/login',
      body: {'code': code},
      parser: (json) {
        print('DEBUG: WeChatService 解析响应: $json');
        if (json['success'] != true) {
          throw ApiException(json['message'] ?? '获取用户信息失败');
        }
        return json;
      },
      requireToken: false,
    );
    print('DEBUG: WeChatService 解析后的响应: $response');
    final result = WeChatAuthResult.fromJson(response);
    print('DEBUG: WeChatService 创建的WeChatAuthResult: success=${result.success}, userInfo=${result.userInfo != null ? "有数据" : "无数据"}');
    return result;
  }
} 